<?php

namespace App\Helper;


class StringHelper
{
    /**
     * Name: getSmsCode
     * Desc:
     *
     * Author: wangxj
     * Date: 2018/11/27
     *
     * @param int $length
     *
     * @return int|string
     */
    public static function getFromPrefix($str, $prefix)
    {
        if (empty($str) || empty($prefix))
        {
            return false;
        }

        if (substr($str, 0, strlen($prefix)) === $prefix)
        {
            return substr($str,strlen($prefix));
        }

        return false;
    }

    /**
     * Name: pickGoodsName
     * Desc: 去掉药物名称中的说明文字
     *
     * Author: wangxj
     * Date: 2019/1/8
     *
     * @param $name
     *
     * @return mixed
     */
    public static function pickGoodsName($name)
    {
        // 删除减号后面的文字
        $newName = explode("-", $name);

        // 删除括号后面的文字
        $newName = explode("(", $newName[0]);
        $newName = explode("（", $newName[0]);

        return trim($newName[0]);
    }

    // 返回是否需要全拼检索
    public static function isNeedFullPinyin($search)
    {
        // 拼音表
        $pylist = array(
            'ai', 'an', 'ang', 'ao',
            'ba', 'bai', 'ban', 'bang', 'bao', 'bei', 'ben', 'beng', 'bi', 'bian', 'biao', 'bie', 'bin', 'bing', 'bo', 'bu',
            'ca', 'cai', 'can', 'cang', 'cao', 'ce', 'ceng', 'cha', 'chai', 'chan', 'chang', 'chao', 'che', 'chen', 'cheng', 'chi', 'chong', 'chou', 'chu', 'chuai', 'chuan', 'chuang', 'chui', 'chun', 'chuo', 'ci', 'cong', 'cou', 'cu', 'cuan', 'cui', 'cun', 'cuo',
            'da', 'dai', 'dan', 'dang', 'dao', 'de', 'deng', 'di', 'dian', 'diao', 'die', 'ding', 'diu', 'dong', 'dou', 'du', 'duan', 'dui', 'dun', 'duo',
            'en', 'er',
            'fa', 'fan', 'fang', 'fei', 'fen', 'feng', 'fo', 'fou', 'fu',
            'ga', 'gai', 'gan', 'gang', 'gao', 'ge', 'gei', 'gen', 'geng', 'gong', 'gou', 'gu', 'gua', 'guai', 'guan', 'guang', 'gui', 'gun', 'guo',
            'ha', 'hai', 'han', 'hang', 'hao', 'he', 'hei', 'hen', 'heng', 'hong', 'hou', 'hu', 'hua', 'huai', 'huan', 'huang', 'hui', 'hun', 'huo',
            'ji', 'jia', 'jian', 'jiang', 'jiao', 'jie', 'jin', 'jing', 'jiong', 'jiu', 'ju', 'juan', 'jue', 'jun',
            'ka', 'kai', 'kan', 'kang', 'kao', 'ke', 'ken', 'keng', 'kong', 'kou', 'ku', 'kua', 'kuai', 'kuan', 'kuang', 'kui', 'kun', 'kuo',
            'la', 'lai', 'lan', 'lang', 'lao', 'le', 'lei', 'leng', 'li', 'lia', 'lian', 'liang', 'liao', 'lie', 'lin', 'ling', 'liu', 'long', 'lou', 'lu', 'lv', 'luan', 'lue', 'lun', 'luo',
            'ma', 'mai', 'man', 'mang', 'mao', 'me', 'mei', 'men', 'meng', 'mi', 'mian', 'miao', 'mie', 'min', 'ming', 'miu', 'mo', 'mou', 'mu',
            'na', 'nai', 'nan', 'nang', 'nao', 'ne', 'nei', 'nen', 'neng', 'ni', 'nian', 'niang', 'niao', 'nie', 'nin', 'ning', 'niu', 'nong', 'nu', 'nv', 'nuan', 'nue', 'nuo',
            'ou',
            'pa', 'pai', 'pan', 'pang', 'pao', 'pei', 'pen', 'peng', 'pi', 'pian', 'piao', 'pie', 'pin', 'ping', 'po', 'pu',
            'qi', 'qia', 'qian', 'qiang', 'qiao', 'qie', 'qin', 'qing', 'qiong', 'qiu', 'qu', 'quan', 'que', 'qun',
            'ran', 'rang', 'rao', 're', 'ren', 'reng', 'ri', 'rong', 'rou', 'ru', 'ruan', 'rui', 'run', 'ruo',
            'sa', 'sai', 'san', 'sang', 'sao', 'se', 'sen', 'seng', 'sha', 'shai', 'shan', 'shang', 'shao', 'she', 'shen', 'sheng', 'shi', 'shou', 'shu', 'shua', 'shuai', 'shuan', 'shuang', 'shui', 'shun', 'shuo', 'si', 'song', 'sou', 'su', 'suan', 'sui', 'sun', 'suo',
            'ta', 'tai', 'tan', 'tang', 'tao', 'te', 'teng', 'ti', 'tian', 'tiao', 'tie', 'ting', 'tong', 'tou', 'tu', 'tuan', 'tui', 'tun', 'tuo',
            'wa', 'wai', 'wan', 'wang', 'wei', 'wen', 'weng', 'wo', 'wu',
            'xi', 'xia', 'xian', 'xiang', 'xiao', 'xie', 'xin', 'xing', 'xiong', 'xiu', 'xu', 'xuan', 'xue', 'xun',
            'ya', 'yan', 'yang', 'yao', 'ye', 'yi', 'yin', 'ying', 'yo', 'yong', 'you', 'yu', 'yuan', 'yue', 'yun',
            'za', 'zai', 'zan', 'zang', 'zao', 'ze', 'zei', 'zen', 'zeng', 'zha', 'zhai', 'zhan', 'zhang', 'zhao', 'zhe', 'zhen', 'zheng', 'zhi', 'zhong', 'zhou', 'zhu', 'zhua', 'zhuai', 'zhuan', 'zhuang', 'zhui', 'zhun', 'zhuo', 'zi', 'zong', 'zou', 'zu', 'zuan', 'zui', 'zun', 'zuo'
        );

        $result    = false;
        $searchLen = strlen($search);
        if ($searchLen <= 1 || $searchLen > 6)
        {
            $result = true;
        }
        else
        {
            if (strpbrk($search, 'iuv') !== false)
            {
                $result = true;
            }
            else
            {
                foreach ($pylist as $py)
                {
                    if (strpos($search, $py) !== false)
                    {
                        $result = true;
                        break;
                    }
                }
            }
        }

        return $result;
    }

    //对比版本号
    public static function cmpVerString($str1,$str2)
    {
        $strVer1 = self::changeVerString($str1);
        $strVer2 = self::changeVerString($str2);

        return  strcmp($strVer1,$strVer2);
    }

    private static function changeVerString($strVer)
    {
        $strVer = trim($strVer,".");
        $strVer = trim($strVer," ");

        if(empty($strVer))
        {
            $newVer = '';
        }
        else
        {
            $tmpArray = explode('.',$strVer);
            $newVer = '';
            for($i=0;$i<count($tmpArray);$i++)
            {
                $newVer .= sprintf("%04d",$tmpArray[$i]);
            }
        }

        return $newVer;
    }

    public function hasChineseCharacters($search)
    {
        if (preg_match("/[\x7f-\xff]/", $search))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}